package com.shujia.filter;

import com.shujia.utils.HbaseTool;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * 三大类过滤器：
 * 1. 比较过滤器
 * 2. 专用过滤器
 * 3. 包装过滤器
 * <p>
 * 包装过滤器：
 */
public class HBaseBaoZhuangFilter {
    public static void main(String[] args) throws Exception{
//        xuQiu1();
        xuQiu2();
    }

    public static void xuQiu2() throws Exception{
        //WhileMatchFilter
        //获取表对象
        Table students2 = HbaseTool.getOneTable("students2");
        Scan scan = new Scan();

        ValueFilter valueFilter = new ValueFilter(CompareOperator.NOT_EQUAL, new BinaryPrefixComparator(Bytes.toBytes("黄")));

        //创建WhileMatchFilter
        WhileMatchFilter whileMatchFilter = new WhileMatchFilter(valueFilter);

        //设置过滤器
        //Scan setFilter(Filter filter)
        scan.setFilter(whileMatchFilter);

        ResultScanner studentsScanner = students2.getScanner(scan);
        for (Result rs : studentsScanner) {
            HbaseTool.printResult(rs);
        }

        HbaseTool.close();

    }

    public static void xuQiu1() throws Exception{
        //SkipFilter
        //获取表对象
        Table students2 = HbaseTool.getOneTable("students2");
        Scan scan = new Scan();

        ValueFilter valueFilter = new ValueFilter(CompareOperator.NOT_EQUAL, new SubstringComparator("理科"));

        //创建跳过过滤器
        //SkipFilter(Filter filter)
        SkipFilter skipFilter = new SkipFilter(valueFilter);


        //设置过滤器
        //Scan setFilter(Filter filter)
        scan.setFilter(skipFilter);

        ResultScanner studentsScanner = students2.getScanner(scan);
        for (Result rs : studentsScanner) {
            HbaseTool.printResult(rs);
        }

        HbaseTool.close();
    }
}
